﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// RoutePageBuild 的摘要说明
    /// </summary>
    public class RoutePageBuild : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string routeid = context.Request["id"];
                Route_BLL routeBLL = new Route_BLL();
                ROUTE route = routeBLL.GetModel(routeid);

                /*
                 * 生成静态页面
                 */
                string templateString = Util.GetTemplateFileContent(context.Server.MapPath("~/Pages/Template_Route_Detail.html"));
                templateString = templateString.Replace("<!--ROUTE_NAME-->", route.ROUTE_NAME);
                templateString = templateString.Replace("<!--ROUTE_DES-->", route.ROUTE_DES);
                templateString = templateString.Replace("<!--ROUTE_CHENGDU-->", route.ROUTE_CHENGDU + "");
                templateString = templateString.Replace("<!--ROUTE_BEIJING-->", route.ROUTE_BEIJING + "");
                templateString = templateString.Replace("<!--ROUTE_ELSECITY-->", route.ROUTE_ELSECITY);

                //打印按钮
                templateString = templateString.Replace("<!---- printLink --->", "<a href='RoutePrint.aspx?routeid=" + routeid + "' >");


                #region 路线特色及推荐图片
                Characteristic_BLL ccsBLL = new Characteristic_BLL();
                List<CHARACTERISTIC> ccs = ccsBLL.GetModels(string.Format("SELECT  CHARACTERISTIC_ID,CHARACTERISTIC_NAME,CHARACTERISTIC_DES FROM CHARACTERISTIC WHERE CHARACTERISTIC_ID IN (SELECT CHARACTERISTIC_ID FROM ROUTE_CHARACTERISTIC WHERE ROUTE_ID='{0}')", route.ROUTE_ID));
                Photo_BLL photoBLL = new Photo_BLL();
                List<PHOTO> photoList = photoBLL.GetModels(string.Format("SELECT PHOTO_ID,PHOTO_NAME,PHOTO_URL,PHOTO_DES,PHOTO_HOT,PHOTO_COVER FROM PHOTO WHERE PHOTO_ID IN (SELECT PHOTO_ID FROM ROUTE_GALLERY WHERE ROUTE_ID='{0}')", route.ROUTE_ID));
                string ccspics = RouteFactory.CreateCharacteristicAndPics(ccs, photoList);
                templateString = templateString.Replace("<!--ROUTE_RECOMMEND_PIC_AND_CHARACTERISTIC-->", ccspics);
                #endregion

                #region 路线套餐包
                Package_BLL packageBLL = new Package_BLL();
                PACKAGE package = packageBLL.GetModelBySQL(string.Format("SELECT PACKAGE_ID,PACKAGE_DES,PACKAGE_INCLUDE,PACKAGE_EXCLUDE FROM PACKAGE WHERE PACKAGE_ID IN (SELECT PACKAGE_ID FROM ROUTE_PACKAGE WHERE ROUTE_ID='{0}')", route.ROUTE_ID));
                templateString = templateString.Replace("<!--ROUTE_PACKAGE_DES-->", package.PACKAGE_DES);
                templateString = templateString.Replace("<!--ROUTE_PACKAGE_INCLUDE-->", package.PACKAGE_INCLUDE);
                templateString = templateString.Replace("<!--ROUTE_PACKAGE_EXCLUDE-->", package.PACKAGE_EXCLUDE);
                #endregion

                #region 路线酒店
                Hotel_BLL hotelBLL = new Hotel_BLL();
                List<HOTEL> hotelss = hotelBLL.GetModels(string.Format("SELECT HOTEL_ID,HOTEL_NAME,HOTEL_DES,HOTEL_ADDRESS,HOTEL_WEBSITE,HOTE_DETAIL FROM HOTEL WHERE HOTEL_ID IN (SELECT HOTE_ID FROM ROUTE_HOTEL WHERE ROUTE_ID='{0}')", route.ROUTE_ID));
                templateString = templateString.Replace("<!--ROUTE_HOTEL-->", RouteFactory.CreateHotels(hotelss));
                #endregion

                #region 路线特色项目
                SpecialItem_BLL spItemBLL = new SpecialItem_BLL();
                List<SPECIALITEM> spItems = spItemBLL.GetModels(string.Format("SELECT SPECIALITEM_ID,SPECIALITEM_NAME,SPECIALITEM_PRICE,SPECIALITEM_DES,SPECIALITEM_COVER FROM SPECIALITEM WHERE SPECIALITEM_ID IN (SELECT SPECIALITEM_ID FROM ROUTE_SPECIALITEM WHERE ROUTE_ID='{0}')", route.ROUTE_ID));
                templateString = templateString.Replace("<!--ROUTE_SPECIALITEM-->", RouteFactory.CreateSpecialItem(spItems));
                #endregion

                #region 路线行程
                Trip_BLL tripBLL = new Trip_BLL();
                List<TRIP> tripList = tripBLL.GetModels(string.Format("SELECT TRIP_ID,TRIP_NAME,TRIP_FOOD,TRIP_DES FROM TRIP WHERE TRIP_ID IN (SELECT TRIP_ID FROM ROUTE_TRIP WHERE ROUTE_ID='{0}')", route.ROUTE_ID));
                templateString = templateString.Replace("<!--ROUTE_TRIP-->", RouteFactory.CreateTrips(tripList));
                #endregion

                #region 推荐线路
                Route_BLL relateRouteBLL = new Route_BLL();
                List<ROUTE> relatesRoutes = relateRouteBLL.GetModels(string.Format("select Top 6 ROUTE_ID,ROUTE_NAME,ROUTE_DES,ROUTE_CHENGDU,ROUTE_BEIJING,ROUTE_ELSECITY,ROUTE_CHARACTERISTIC,ROUTE_DAYS,ROUTE_PRICE,ROUTE_DATE_START,ROUTE_DATE_END,ROUTE_HOT,ROUTE_BAK,ROUTE_ELABORATE,ROUTE_ORIGINAL FROM ROUTE WHERE ROUTE_HOT>90 AND ROUTE_ID!='{0}' ORDER BY ROUTE_HOT DESC", route.ROUTE_ID));
                templateString = templateString.Replace("<!--RELATE_ROUTE_TRIP-->", RouteFactory.CreateRelatedRoutes(relatesRoutes
));
                #endregion

                #region 路线航班
                RouteFlight_BLL routeFlightBLL = new RouteFlight_BLL();
                ROUTEFLIGHT routeFlight = routeFlightBLL.GetModel(routeid);
                templateString = templateString.Replace("<!--ROUTE_FLIGHT_CHENGDU-->", RouteFactory.CreateRouteFlightCD(routeFlight
));
                templateString = templateString.Replace("<!--ROUTE_FLIGHT_BEIJING-->", RouteFactory.CreateRouteFlightBJ(routeFlight
));
                templateString = templateString.Replace("<!--ROUTE_FLIGHT_ELSE-->", routeFlight.ROUTE_ELSE);
                #endregion 

                bool success = Util.GenerateStaticHTML(templateString, context.Server.MapPath(string.Format("~/Pages/{0}.html", route.ROUTE_ID)));

                if (!success)
                    throw new Exception("生成静态页面错误");

                info.Success = true;
                info.Title = "成功";
                info.Tip = "生成静态页成功";
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("生成静态页失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "生成静态页失败";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}